package com.lanelu.hencoderplusstudy

import android.animation.*
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Interpolator
import android.graphics.PointF
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.view.animation.LinearInterpolator
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.core.graphics.drawable.toDrawable
import com.kongqw.network.monitor.NetworkMonitorManager
import com.kongqw.network.monitor.enums.NetworkState
import com.kongqw.network.monitor.interfaces.NetworkMonitor
import com.lanelu.hencoderplusstudy.databinding.ActivityMainBinding
import com.lanelu.hencoderplusstudy.view.ProvinceEvaluator
import com.lanelu.hencoderplusstudy.view.dp
import com.lanelu.lib_common.base.BaseActivity
import com.lanelu.lib_common.utils.LogUtils
import com.lanelu.lib_common.utils.ScreenUtils


class MainActivity : BaseActivity() {

    private lateinit var mBinding: ActivityMainBinding

    override fun getLayoutResId(): Int {
        return R.layout.activity_main
    }

    override fun initViewModel() {

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = getBinding() as ActivityMainBinding;
        mBinding.statusBar.layoutParams = LinearLayoutCompat.LayoutParams(
            LinearLayoutCompat.LayoutParams.MATCH_PARENT,
            ScreenUtils.getStatusBarHeight(mContext)
        )

        //CircleView
//        val circleRadiusAnimator = ObjectAnimator.ofFloat(mBinding.view, "radius", 200.dp)
//        circleRadiusAnimator.startDelay = 500
//        circleRadiusAnimator.duration = 1000
//        circleRadiusAnimator.start()

        //CamaraView
//        val bottomFlipAnimator = ObjectAnimator.ofFloat(mBinding.view, "bottomFlip", 60f)
////        bottomFlipAnimator.startDelay = 1000
////        bottomFlipAnimator.duration = 1000
////        bottomFlipAnimator.start()
//
//        val flipRotationAnimator = ObjectAnimator.ofFloat(mBinding.view, "flipRotation", 270f)
////        flipRotationAnimator.startDelay = 200
////        flipRotationAnimator.duration = 1000
////        flipRotationAnimator.start()
//
//        val topFlipAnimator = ObjectAnimator.ofFloat(mBinding.view, "topFlip", -60f)
////        topFlipAnimator.startDelay = 200
////        topFlipAnimator.duration = 1000
////        topFlipAnimator.start()
//
//        val animatorSet = AnimatorSet()
//        animatorSet.playSequentially(
//            bottomFlipAnimator,
//            flipRotationAnimator,
//            topFlipAnimator
//        )
//        animatorSet.startDelay = 1000
//        animatorSet.duration = 1000
//        animatorSet.start()


        //PropertyValuesHolder练习
//        val bottomFlipHolder = PropertyValuesHolder.ofFloat("bottomFlip", 60f)
//        val flipRotationHolder = PropertyValuesHolder.ofFloat("flipRotation", 270f)
//        val topFlipHolder = PropertyValuesHolder.ofFloat("topFlip", -60f)
//        val holderAnimator = ObjectAnimator.ofPropertyValuesHolder(
//            mBinding.view,
//            bottomFlipHolder,
//            flipRotationHolder,
//            topFlipHolder
//        )
//        holderAnimator.startDelay = 1000
//        holderAnimator.duration = 2000
//        holderAnimator.start()

//        //keyframe关键帧练习
//        val length = 200.dp
//        val keyframe1 = Keyframe.ofFloat(0f, 0f * length)
//        val keyframe2 = Keyframe.ofFloat(0.2f, 1.5f * length)
//        val keyframe3 = Keyframe.ofFloat(0.8f, 0.6f * length)
//        val keyframe4 = Keyframe.ofFloat(1f, 1f * length)
//        val keyframeHolder = PropertyValuesHolder.ofKeyframe(
//            "translationX",
//            keyframe1,
//            keyframe2,
//            keyframe3,
//            keyframe4
//        )
//        val animator = ObjectAnimator.ofPropertyValuesHolder(mBinding.view, keyframeHolder)
//        animator.startDelay = 1000
//        animator.duration = 3000
//        animator.start()

//        //Interpolator插值器练习
//        val animator = ObjectAnimator.ofFloat(mBinding.view, "translationX", 200.dp)
//        animator.interpolator = AccelerateDecelerateInterpolator()
//        animator.setEvaluator(FloatEvaluator())
//        animator.startDelay = 1000
//        animator.duration = 3000
//        animator.start()

        //TypeEvaluator
        //PointFView
//        val animator = ObjectAnimator.ofObject(
//            mBinding.view,
//            "point",
//            PointFEvaluator(),
//            PointF(100.dp, 200.dp)
//        )
//        animator.startDelay = 1000
//        animator.duration = 3000
//        animator.start()

        //ProvinceView
//        val animator =
//            ObjectAnimator.ofObject(mBinding.view, "province", ProvinceEvaluator(), "澳门特别行政区")
//        animator.startDelay = 1000
//        animator.duration = 10000
//        animator.start()


        NetworkMonitorManager.getInstance().register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        NetworkMonitorManager.getInstance().unregister(this)
    }

    //监听网络状态变化
    @NetworkMonitor
    fun onNetWorkStateChange(networkState: NetworkState) {
        when (networkState) {
            NetworkState.NONE -> {
                // TODO 暂无网络
                LogUtils.e("暂无网络")
            }
            NetworkState.WIFI -> {
                // TODO WIFI网络
                LogUtils.e("WIFI网络")
            }
            NetworkState.CELLULAR -> {
                // TODO 蜂窝网络
                LogUtils.e("蜂窝网络")
            }
        }
    }

    class PointFEvaluator : TypeEvaluator<PointF> {

        override fun evaluate(fraction: Float, startValue: PointF, endValue: PointF): PointF {
            val startX = startValue.x
            val endX = endValue.x
            val currentX = startX + (endX - startX) * fraction
            val startY = startValue.y
            val endY = endValue.y
            val currentY = startY + (endY - startY) * fraction
            return PointF(currentX, currentY)
        }

    }

}